﻿using Bookie.Common;
using Bookie.Model;
using DataServices.Services;
using GalaSoft.MvvmLight;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;

namespace Bookie.ViewModel
{
    public class HomeViewModel : ViewModelBase, IHomeViewModel
    {
        bool isShowLoading = true;
        public bool IsShowLoading
        {
            get
            {
                return isShowLoading;
            }
            set
            {
                Set(ref isShowLoading, value);
            }
        }
        ObservableCollection<Offer> listOffers;
        public ObservableCollection<Offer> Offers
        {
            get
            {
                if (listOffers == null)
                {
                    listOffers = new ObservableCollection<Offer>();
                }
                return listOffers;
            }
            set
            {
                Set(ref listOffers, value);
            }
        }
        ObservableCollection<Banner> listBanner;
        public ObservableCollection<Banner> Banners
        {
            get
            {
                if (listBanner == null)
                {
                    listBanner = new ObservableCollection<Banner>();
                }
                return listBanner;
            }
            set
            {
                Set(ref listBanner, value);
            }
        }
        IRestfullServices restfullServices;

        public HomeViewModel(IRestfullServices restfullServices)
        {
            this.restfullServices = restfullServices;
            restfullServices.SetBaseAddress("http://www.everybookie.co.uk.gridhosted.co.uk");
            GetOffer("100", "0");
        }
        public async Task GetOffer(string limit, string offset)
        {
            IsShowLoading = true;
            var url = string.Format("/betapp/api/get_offers?limit={0}&offset={1}", limit, offset);
            var getDataOffer = await restfullServices.PostAsync(url, null);
            if (getDataOffer != null)
            {
                var paserData = JsonSerialization.s_Deserialize<OffersData>(getDataOffer);
                if (paserData != null)
                {
                    if (Offers.Count == 0)
                    {
                        Offers = new ObservableCollection<Offer>(paserData.offers);
                    }
                    else
                    {
                        foreach (var m in paserData.offers)
                        {
                            Offers.Add(m);
                        }
                    }
                    Banners = new ObservableCollection<Banner>(paserData.banner);
                }
            }
            else
            {
                await new MessageDialog("Get data failed, please try again", "error").ShowAsync();
            }
            IsShowLoading = false;
        }
    }
}
